package com.lanyou.cook.web.userManagement;

import java.util.Collection;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.JsonArray;
import com.google.gson.JsonPrimitive;
import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.userManagement.Permission;
import com.lanyou.cook.service.security.PermissionService;
import com.lanyou.cook.service.security.UserSessionService;

/**
 * 权限管理相关业务控制层
 * @author 仇伟业
 *
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/permissions")
public class PermissionController {

	@Autowired
	private PermissionService permissionService;

	// 获取当前登录用户的所有权限
	/**
	 * 获取当前登录用户的所有权限
	 * @return 类型:String 返回当前用户的所有权限
	 */
	@RequestMapping(value = "/search/findPermissionsOfCurrentUser", produces = "application/json")
	public String findPermissionsOfCurrentUser() {
		Collection<Permission> resultPermissions = null;
		String userCode = UserSessionService.getCurrentAuthenticatedUserCode();
		if(userCode != null){
			resultPermissions = permissionService.findPermissionsOfUser(userCode);
		}
		
		if(resultPermissions == null){
			resultPermissions = CollectionUtils.emptyCollection();
		}
		
		JsonArray result = new JsonArray();
		resultPermissions.stream()
			.map(p -> p.getPopedomCode())
			.forEach(p -> result.add(new JsonPrimitive(p)));
		
		return result.toString();
	}

}
